/**
 * 
 */
package truerefactor.graph;

/**
 * Cardinality - Class used to decorate and Edge with multiple weights according
 * to UML notions of min and max cardinality and source and destination
 * cardinality
 * 
 * @author Isaac Griffith
 */
public class Cardinality {

    /**
     * Minimum value
     */
    private String min;
    /**
     * Maximum value
     */
    private String max;

    /**
     * Creates a new instance of Cardinality with default value of 1 for both
     * min and max cardinality
     */
    public Cardinality()
    {
        this("1", "1");
    }

    /**
     * Creates a new Cardinality with the provided min and max values
     * 
     * @param min
     *            minimum cardinality
     * @param max
     *            maximum cardinality
     */
    public Cardinality(String min, String max)
    {
        if (!min.isEmpty() && !max.isEmpty())
        {
            this.min = this.max = "1";
        }
        else if (min.isEmpty() && !max.isEmpty())
        {
            this.min = "0";
            this.max = max;
        }
        else if (max.isEmpty() && !min.isEmpty())
        {
            this.min = min;
            this.max = "*";
        }
        else
        {
            this.min = min;
            this.max = max;
        }
    }

    /**
     * @return the minimum cardinality value
     */
    public String getMin()
    {
        return min;
    }

    /**
     * Sets the minimum cardinality value to the string representation of the
     * provided value
     * 
     * @param min
     *            the minimum cardinality
     */
    public void setMin(String min)
    {
        this.min = min;
    }

    /**
     * @return the maximum cardinality value
     */
    public String getMax()
    {
        return max;
    }

    /**
     * Sets the maximim cardinality value to the string representation of the
     * provided value
     * 
     * @param max
     *            the maximum cardinality
     */
    public void setMax(String max)
    {
        this.max = max;
    }

    /**
     * @return the complete representation of the cardinality
     */
    public String getCompleteCardinality()
    {

        if (!min.equals(max))
        {
            return String.format("%s .. %s", min, max);
        }
        else
        {
            return max;
        }
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#clone()
     */
    @Override
    protected Object clone() throws CloneNotSupportedException
    {
        return new Cardinality(new String(min), new String(max));
    }
}
